package com.muqi.app.qlearn.student.ui;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.fragments.BannerBodyFragment;
import com.muqi.app.qlearn.modles.Body;
import com.muqi.app.qlearn.modles.PersonBean;
import com.muqi.app.qlearn.student.BaseFragmentActivity;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.qlearn.student.R.layout;
import com.weidongjian.meitu.wheelviewdemo.view.LoopView;
import com.weidongjian.meitu.wheelviewdemo.view.OnItemSelectedListener;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.TextView;

public class BodyActivity extends BaseFragmentActivity implements
		OnClickListener {
	private TextView tv_save;
	private TextView tv_heigt;
	private TextView tv_weight;
	private LoopView loopView1;
	private LoopView loopView2;
	private List<String> height_integer;
	private List<String> height_float;
	private List<String> weight_integer;
	private List<String> weight_float;

	private boolean isHeight = true;
	private String current_height;
	private String current_height_float;
	private int heigt_position;
	private int heigt_float_position;

	private String current_weight;
	private String current_weight_float;
	private int weight_position;
	private int weight_float_position;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_body);
		// mSpUtil.
	}

	@Override
	protected void onInit() {

		height_integer = new ArrayList<String>();
		for (int i = 0; i < 200; i++) {
			height_integer.add((i + ""));
		}
		height_float = new ArrayList<String>();
		for (int i = 0; i < 10; i++) {
			height_float.add("." + i + " cm");
		}
		weight_integer = new ArrayList<String>();
		for (int i = 0; i < 100; i++) {
			weight_integer.add(i + "");
		}
		weight_float = new ArrayList<String>();
		for (int i = 0; i < 10; i++) {
			weight_float.add("." + i + " kg");
		}
		tv_heigt = (TextView) findViewById(R.id.body_tv_height);
		tv_weight = (TextView) findViewById(R.id.body_tv_weight);
		tv_save = (TextView) findViewById(R.id.body_save);
		loopView1 = (LoopView) findViewById(R.id.loopView1);
		loopView2 = (LoopView) findViewById(R.id.loopView2);

		tv_heigt.setOnClickListener(this);
		tv_weight.setOnClickListener(this);
		tv_save.setOnClickListener(this);

		loopView1.setItems(height_integer);
		loopView1.setTextSize(30);
		loopView1.setViewPadding(100, 20, 20, 20);
		loopView1.setNotLoop();
		loopView1.setListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(int index) {
				if (isHeight) {
					current_height = height_integer.get(index);
					heigt_position = index;
					tv_heigt.setText(current_height + current_height_float);
				} else {
					current_weight = weight_integer.get(index);
					weight_position = index;
					tv_weight.setText(current_weight + current_weight_float);
				}

			}
		});

		loopView2.setItems(height_float);
		loopView2.setTextSize(30);
		loopView2.setViewPadding(20, 20, 100, 20);
		loopView2.setNotLoop();
		loopView2.setListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(int index) {
				if (isHeight) {
					current_height_float = height_float.get(index);
					heigt_float_position = index;
					tv_heigt.setText(current_height + current_height_float);
				} else {
					current_weight_float = weight_float.get(index);
					weight_float_position = index;
					tv_weight.setText(current_weight + current_weight_float);
				}

			}
		});

	}

	@Override
	protected void onData() {
		loadData();

	}

	/**
	 * 用来初始化选中的数据
	 */
	private void initSelection() {
		// TODO Auto-generated method stub
		for (int i = 0; i < height_integer.size(); i++) {
			if (current_height.equals(height_integer.get(i))) {
				heigt_position = i;
				loopView1.setInitPosition(i);
				break;
			}
		}
		for (int i = 0; i < height_float.size(); i++) {
			if (current_height_float.equals(height_float.get(i))) {
				heigt_float_position = i;
				loopView2.setInitPosition(i);
				break;
			}
		}

		for (int i = 0; i < weight_integer.size(); i++) {
			if (current_weight.equals(weight_integer.get(i))) {
				weight_position = i;
				break;
			}
		}
		for (int i = 0; i < weight_float.size(); i++) {
			if (current_weight_float.equals(weight_float.get(i))) {
				weight_float_position = i;
				break;
			}
		}
		loopView1.setItems(height_integer);

		loopView2.setItems(height_float);
	}

	public void onBack(View view) {
		finish();
	}

	@Override
	public void onClick(View arg0) {
		switch (arg0.getId()) {
		case R.id.body_save:

			updateBody();
			finish();

			break;
		case R.id.body_tv_height:

			isHeight = true;

			loopView1.setItems(height_integer);
			loopView2.setItems(height_float);
			loopView1.setInitPosition(heigt_position);
			loopView2.setInitPosition(heigt_float_position);

			break;
		case R.id.body_tv_weight:
			isHeight = false;

			loopView1.setItems(weight_integer);
			loopView2.setItems(weight_float);
			loopView1.setInitPosition(weight_position);
			loopView2.setInitPosition(weight_float_position);

			break;
		default:
			break;
		}

	}

	/**
	 * 更新用户的身体数据
	 */
	private void updateBody() {
		if (NetWorkUtils.isNetworkAvailable(this)) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("token", mSpUtil.getToken());
			map.put("user_id", mSpUtil.getUserId());
			map.put("weight",
					current_weight + current_weight_float.replace(" kg", ""));
			map.put("height",
					current_height + current_height_float.replace(" cm", ""));
			String requestApi = ParamsUtils.buildParams(
					NetWorkApi.UPDATE_BODY_API, map);
			MyAsyncHttp.get(this, requestApi, new AsyncCallBackListener() {

				@Override
				public void onAsyncCallBack(RESULTCODE resuleCode,
						String request, String result) {
					if (resuleCode == RESULTCODE.SUCCESS) {
						sendBroadcast(new Intent(BannerBodyFragment.BANNER_BODY_CHANGED));
						showToast("保存成功");
					}
				}
			});
		} else {
			ShowToast.showShort(this, R.string.net_break);
		}

	}

	/**
	 * 加载身高和体重
	 */
	private void loadData() {
		if (NetWorkUtils.isNetworkAvailable(this)) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("token", mSpUtil.getToken());
			map.put("user_id", mSpUtil.getUserId());
			String requestApi = ParamsUtils.buildParams(
					NetWorkApi.GET_BODY_API, map);
			MyAsyncHttp.get(this, requestApi, new AsyncCallBackListener() {

				@Override
				public void onAsyncCallBack(RESULTCODE resuleCode,
						String request, String result) {
					//Log.e("===", result);
					if (resuleCode == RESULTCODE.SUCCESS) {
						Body body = ResponseUtils.getBody(mContext, result);

						if (body != null && body.current_detail_userinfo!=null) {
							String height = body.current_detail_userinfo.height;
							String weight = body.current_detail_userinfo.weight;
							int index = height.lastIndexOf(".");
							current_height = height.substring(0, index);
							current_height_float = height.substring(index)
									+ " cm";

							index = weight.lastIndexOf(".");
							current_weight = weight.substring(0, index);
							current_weight_float = weight.substring(index)
									+ " kg";

							tv_heigt.setText(current_height
									+ current_height_float);
							tv_weight.setText(current_weight
									+ current_weight_float);
							initSelection();
						} else {
							current_height = "0";
							current_weight = "0";
							current_height_float = ".0 cm";
							current_weight_float = ".0 kg";
							tv_heigt.setText(current_height
									+ current_height_float);
							tv_weight.setText(current_weight
									+ current_weight_float);
						}

					}
				}
			});
		} else {
			ShowToast.showShort(this, R.string.net_break);
		}

	}
}
